#!/bin/bash
# description:　PCS service deploy
# version:	1.0
# author:	zhangyanbin
# contact:	yanbin_zhang@foxitsoftware.com
#
if [ -z "$TOMCAT_HOME" ]; then
  TOMCAT_HOME=/usr/share/tomcat
fi

if [ ! -d "$TOMCAT_HOME/latest" ]
then
	ln -s /usr/share/tomcat /usr/share/tomcat/latest
fi

if [ ! -d "/userstorage" ]; then
	mkdir /userstorage
	chown -Rf tomcat.tomcat /userstorage
fi

#Init current directory
cd /tmp

#Consont variable
#USER_CENTER_DIR=/tmp/S5_User-Center
#PCS_HOME_DIR=/tmp/S4_User-File-System
#USER_FILESYSTEM_DIR=/tmp/S4_User-File-System
FS_DIR=/tmp/S2_Base-File-System
#SSO_DIR=/tmp/S1_SSO/src/foxit-sso
SCHEDULE_DIR=/tmp/TB-Schedule/tbschedule
#WEBPDF_DIR=/tmp/Web-PDF-API/foxit-webpdf

#Permanently added 'git.oschina.net' (RSA) to the list of known hosts
KNOWN_HOSTS=~/.ssh/known_hosts
if [ ! -e "/root/.ssh" ]; then
    mkdir /root/.ssh
fi

#Need to dudge whether it need to add the rsa
touch $KNOWN_HOSTS && ssh-keyscan -H "git.oschina.net" >> $KNOWN_HOSTS && chmod 600 $KNOWN_HOSTS

#Copy our private rsa key to .ssh directory,and set permission
\cp /vagrant/vagrant/svcconfig/id_rsa/* /root/.ssh/
rsa_keys=( id_rsa_fs id_rsa_tbschedule ) #id_rsa_pcs id_rsa_sso id_rsa_user-center id_rsa_webreader
for i in "${rsa_keys[@]}"
do
    if  [ -e ~/.ssh/$i ]; then
		chmod 400 ~/.ssh/$i
    fi
done

#Git clone project file to local filesystem,service_git_hosts defined in /root/.ssh/config file
service_names=( S2_Base-File-System TB-Schedule ) #Web-PDF-API S1_SSO S4_User-File-System S5_User-Center 
service_git_hosts=( oschina-fs oschina-schedule ) #oschina-webreader oschina-sso oschina-pcs oschina-user
service_branchs=( master vagrant vagrant  master vagrant master )
for i in "${!service_names[@]}"; do
	echo "Git clone or pull ${service_names[$i]}"
	if [  -e "/tmp/${service_names[$i]}/.git" ]; then
		cd /tmp/${service_names[$i]}
	    sudo git checkout ${service_branchs[$i]}
		sudo git pull
	else 
		rm -rf /tmp/${service_names[$i]}
		sudo git clone -b ${service_branchs[$i]} git@${service_git_hosts[$i]}:sumiluxgit_admin/${service_names[$i]}.git /tmp/${service_names[$i]}
	fi
done

#Set the domain to the hosts
domains=( fs.foxitcloud.com sdl.foxitcloud.com ) #webpdf365.foxitcloud.com foxit-pcs-web foxit-sso foxit-user-center-web
for i in "${domains[@]}"
do
	sed -i "/$i/d"  /etc/hosts
	echo "127.0.0.1 $i">>/etc/hosts
done

#Maven compile the java project
\cp /vagrant/vagrant/svcconfig/settings.xml /etc/maven
#cd $SSO_DIR
#rm -rf $SSO_DIR/src/test
#sed -i 's/10.1.103.93/127.0.0.1/g' $SSO_DIR/pom.xml
#sed -i 's/10.1.103.91:30000,10.1.104.91:30000,10.1.105.91:30000/127.0.0.1:27017/g' $SSO_DIR/pom.xml
#sed -i 's/<cas.mongodb.username>foxitcas<\/cas.mongodb.username>/<cas.mongodb.username>mongo<\/cas.mongodb.username>/g' $SSO_DIR/pom.xml
#sed -i 's/<cas.mongodb.password>EPFUmDlZ42N8IguFEQpk2rGGxiebqD<\/cas.mongodb.password>/<cas.mongodb.password>mongo<\/cas.mongodb.password>/g' $SSO_DIR/pom.xml
#mvn clean install  -PPRO -DskipTests
#cd $USER_CENTER_DIR
#sed -i 's/10.1.103.91:30000,10.1.104.91:30000,10.1.105.91:30000/127.0.0.1:27017/g' $USER_CENTER_DIR/pom.xml
#sed -i 's/10.1.103.93:27017/127.0.0.1:27017/g' $USER_CENTER_DIR/pom.xml
#sed -i 's/10.1.104.93:27017/127.0.0.1:27017/g' $USER_CENTER_DIR/pom.xml
#sed -i 's/<com.foxit.user.session.mongodb.username>foxitusercenter<\/com.foxit.user.session.mongodb.username>/<com.foxit.user.session.mongodb.username>mongo<\/com.foxit.user.session.mongodb.username>/g' $USER_CENTER_DIR/pom.xml
#sed -i 's/<com.foxit.user.session.mongodb.password>8CuYzZg6NbL9fUhpgW1W4ttAO7R6yA<\/com.foxit.user.session.mongodb.password>/<com.foxit.user.session.mongodb.password>mongo<\/com.foxit.user.session.mongodb.password>/g' $USER_CENTER_DIR/pom.xml
#mvn clean install  -PPRO -DskipTests
#cd $USER_FILESYSTEM_DIR/src
#mvn clean install  -Pvagrant -DskipTests
#cd $PCS_HOME_DIR/src/foxit-pcs-web
#mvn clean install -DskipTests -Pvagrant
cd $FS_DIR
mvn clean install -DskipTests -Pvagrant
cd $SCHEDULE_DIR
mvn clean install -DskipTests -Pvagrant
#cd $WEBPDF_DIR
#sed -i 's/10.1.103.93/127.0.0.1/g' $WEBPDF_DIR/pom.xml
#sed -i 's/10.1.103.91:30000,10.1.104.91:30000,10.1.105.91:30000/127.0.0.1:27017/g' $WEBPDF_DIR/pom.xml
#sed -i 's/<com.foxit.webpdf.log.level>DEBUG,FILE,MAIL<\/com.foxit.webpdf.log.level>/<com.foxit.webpdf.log.level>ERROR,FILE<\/com.foxit.webpdf.log.level>/g' $WEBPDF_DIR/pom.xml
#mvn clean install -DskipTests -Ppro

#Deploy java project to tomcat,first move app file to tomcat webapps directory,then add Host to tomcat server.xml file
#\cp -r $PCS_HOME_DIR/src/foxit-pcs-web/target/foxit-pcs-web $TOMCAT_HOME/webapps
\cp -r $FS_DIR/foxit-filesystem-web/target/foxit-filesystem-web $TOMCAT_HOME/webapps
#\cp -r $SSO_DIR/target/foxit-sso $TOMCAT_HOME/webapps
#\cp -r $USER_CENTER_DIR/foxit-user-center-web/target/foxit-user-center-web $TOMCAT_HOME/webapps
\cp -r $SCHEDULE_DIR/target/tbschedule $TOMCAT_HOME/webapps
#\cp -r $WEBPDF_DIR/foxit-webpdf-web/target/foxit-webpdf-web $TOMCAT_HOME/webapps


SERVER_FILE=$TOMCAT_HOME/conf/server.xml
POSITION='<\/Engine>'
webapp_dirs=( foxit-filesystem-web tbschedule ) #foxit-webpdf-web foxit-pcs-web foxit-sso foxit-user-center-web
for i in "${!domains[@]}"; do
	REPACE="<Host name=\"${domains[$i]}\" appBase=\"webapps/${webapp_dirs[$i]}\" unpackWARs=\"true\" autoDeploy=\"true\"><Context docBase=\".\" path=\"\" /></Host>"
	sed -i "/${domains[$i]}/d"  $SERVER_FILE
	sed -i "/$POSITION/i $REPACE" $SERVER_FILE
done

#Deploy to Apache httpd,httpcfgs mean the configureation of app in httpd,and then restart httpd
httpdcfgs=( fs.conf sdl.conf foxitcloud.crt foxitcloud.key) #webreader.conf pcs.conf sso.conf user.conf 
for i in "${httpdcfgs[@]}"
do
	if [ ! -e "/etc/httpd/conf.d/$i" ]; then
	    cp /vagrant/vagrant/svcconfig/httpd_config/$i /etc/httpd/conf.d/$i
	fi
done
#Make httpd support https 443
sed -i  "s/^.*Listen 443.*$/Listen 443 http/" /etc/httpd/conf.d/ssl.conf
sed -i "/NameVirtualHost \*:443/d"  /etc/httpd/conf.d/ssl.conf
sed -i "/Listen 443 http/i NameVirtualHost *:443" /etc/httpd/conf.d/ssl.conf
#sudo service httpd restart
